<?php
namespace App\Repos;

use App\Models\Dashboard;
use App\Models\User;
use App\Models\TaskProcess;
use App\Models\TaskService;
use App\Models\TaskDepart;
use App\Models\Client;
use App\Models\ClientUser;

use DB,Log;
use App\Services\Wechat;

class DashboardRepo
{
	
    

    /**
     * 新单推送
     */
    public static function newTaskPush($data)
    {
        // try {
        //     $client = Client::find($data['client_id']);
        //     $msg = $data;
        //     $msg['openid'] = $client->openid;            
        //     $msg['first']  = '您提交的新工单已生成';                 
        //     $msg['remark'] = '点击查看详情。';
        //     $msg['url']    = config('app.url').'/user/task/show/'.$data['id'];
        //     Wechat::sendMsgTpl('baoyang',$msg);
        //     Log::info('新单：推送给用户成功');
              
        // } catch (\Exception $e) {
        //     Log::error('新单推送失败：'.$e->getMessage());
        // }
            
    }

    /**
     * 派工
     */
    public static  function dispatch($data)
    {       
      // dd($data);
        $worker = User::find($data['worker_id']);
        if(empty($worker)) return false;
        $task   = Dashboard::find($data['dashboard_id']);       
        $data['worker_openid'] = $worker->openid;        
        $flag = self::process($data);  

        
        //维修进厂通知 to client
        $msg = $task->toArray(); 
        $msg['car_num'] = $task->car_num;  
        $msg['name']   = $task->customer_name;
        $msg['arrived_at']  = $task->in_date;
        $msg['phone']  = $task->mobile;
        $msg['worker_name']   = $worker->truename; 
        $msg['first']  = '进场维修通知';       
        $msg['url']    = config('app.url').'/user/dashboard/show/'.$task->id;
       
        // 推送消息
         
        $msg['openid'] = $worker->openid;       
        Wechat::sendMsgTpl('new_to_client',$msg);

       
        if($flag) return true;
        return false;
    }

    private static function process($data)
    {        
        $data = array_only($data,['dashboard_id','worker_id','remark']);
        //$data['status'] =  50;  
        //DashboardProcess::create($data);

        $updateData = array_only($data,['worker_id']);       
        $flag = Dashboard::where('id',$data['dashboard_id'])->update($updateData);
        return $flag;
    }


    public static function getLevelArr()
    {
        $arr['1'] = '一般';
        $arr['2'] = '中等';
        $arr['3'] = '紧急';
        return $arr;
    }

    public static function getLevelCss()
    {
        $arr['1'] = 'jiedan_yiban';
        $arr['2'] = 'jiedan';
        $arr['3'] = 'jiedan_jinji';
        return $arr;
    }

   

   

    

	
	
}